package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.employeesync.sapquery.service.SapEmployeeQueryService;
import com.sec.etech.employeesync.service.EmployeeDataSyncService;
import com.sec.etech.ism.service.IsmAppSettingService;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmServiceDefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : 齐马的作品
 * @version : 1.0
 * @date : 2024/8/20 16:26
 * @description:
 */
@Service
@Slf4j
public class IsmQuerySapEmployeeByWerksImpl implements IsmMethodService {

    @Resource
    private IsmAppSettingService ismAppSettingService;
    @Resource
    private IsmServiceDefService ismServiceDefService;
    @Resource
    private IsmCommonService ismCommonService;
    @Autowired
    private EmployeeDataSyncService employeeDataSyncService;
    @Autowired
    private SapEmployeeQueryService sapEmployeeQueryService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {
        return sapEmployeeQueryService.querySapEmployee(sysCode, serviceCode, businessParams);
    }

//    private String querySapEmployeeByWerks(String sysCode, String serviceCode, JSONObject businessParams) {
//        String msg = "success";
//
//        try {
//            log.info("businessParams===>{}", businessParams);
//
//            JSONArray iwerks = businessParams.getJSONArray("iwerks");
//
//            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
//            IsmServiceDef serviceDef = ismServiceDefService.getByCode(serviceCode);
//
//            String url = "http://webqas08.shanghai-electric.com/dce-api/sjram-service/api/do/TECHTALENT/Q32/231/ZBAPI_EMPLOYEE_GETLIST_TECH";
//            String appKey = appSetting.getAppKey2();
//
//            JSONObject sapParams = new JSONObject();
//
//            JSONArray itDatumArray = new JSONArray();
//            JSONObject itDatum = new JSONObject();
//            itDatum.put("SIGN", "I");
//            itDatum.put("OPTION", "BT");
//            itDatum.put("LOW", "19491001");
//            itDatum.put("HIGH", "99991231");
//            itDatumArray.add(itDatum);
//
//            sapParams.put("IT_DATUM", itDatumArray);
//
//            // 遍历iwerks数组
//            JSONArray resultArray = new JSONArray();
//            for (int i = 0; i < iwerks.size(); i++) {
//                String iwerksString = iwerks.getString(i);
//                sapParams.put("I_WERKS", iwerksString);
//
//                JSONObject bodyObj = SapSignatureUtil.buildRequestParams(sapParams, appKey);
//                String body = JSONObject.toJSONString(bodyObj);
//                int timeout = JSONObject.parseObject(serviceDef.getParams()).getInteger("timeout");
//                long startTime = System.currentTimeMillis();
//                String result = HttpUtil.post(url, body, timeout * 60 * 1000);
//
//                log.info("SAP查询公司==>{}", iwerksString);
//                log.info("SAP系统{}接口耗时：{}ms。", "调用SAP接口批量集团员工信息", System.currentTimeMillis() - startTime);
//                log.info("SAP返回值==>{}", result);
//
//                resultArray.add(JSONObject.parseObject(result)); // 将每次结果存储到结果数组中
//            }
//
//            // 将所有结果数据同步
//            for (int j = 0; j < resultArray.size(); j++) {
//                employeeDataSyncService.syncEmployeeData(resultArray.getJSONObject(j));
//            }
//
//        } catch (Exception e) {
//            msg = "error";
//            log.error("Error occurred while querying SAP employee data: ", e);
//        }
//
//        return msg;
//    }

    @Override
    public String methodName() {
        return "querySapEmployeeByWerks";
    }
}
